<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="点位名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入点位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入点位位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ip" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入ip"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:coldefinetable:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:coldefinetable:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:coldefinetable:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          v-hasPermi="['business:coldefinetable:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:coldefinetable:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coldefinetableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="点位编号" align="center" prop="code" />
      <el-table-column label="点位名称" align="center" prop="name" />
      <el-table-column label="点位英文名称" align="center" prop="variableEnName" />
      <el-table-column label="位置" align="center" prop="area" />
      <el-table-column label="点位描述" align="center" prop="description" />
      <el-table-column label="地址" align="center" prop="src" />
      <el-table-column label="数据类型" align="center" prop="vartype" />
      <el-table-column label="ip" align="center" prop="ip" />
      <el-table-column label="备注" align="center" prop="remark2" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:coldefinetable:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:coldefinetable:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改点位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="点位编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入点位编号" />
        </el-form-item>
        <el-form-item label="点位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入点位名称" />
        </el-form-item>
        <el-form-item label="点位英文名称" prop="variableEnName">
          <el-input v-model="form.variableEnName" placeholder="请输入点位英文名称" />
        </el-form-item>
        <el-form-item label="位置" prop="area">
          <el-input v-model="form.area" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="点位描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入点位描述" />
        </el-form-item>
        <el-form-item label="地址" prop="src">
          <el-input v-model="form.src" placeholder="请输入点位地址" />
        </el-form-item>
        <el-form-item label="ip" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入ip" />
        </el-form-item>
        <el-form-item label="备注" prop="remark2">
          <el-input v-model="form.remark2" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="导入点位" :visible.sync="importOpen" width="400px" append-to-body>
      <el-upload
        :action="importUrl"
        :show-file-list="false"
        :on-success="handleImportSuccess"
        :before-upload="beforeUpload"
      >
        <el-button type="primary">选择文件上传</el-button>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="importOpen = false">关 闭</el-button>
      </div>
    </el-dialog>


  </div>

</template>

<script>
import { listColdefinetable, getColdefinetable, delColdefinetable, addColdefinetable, updateColdefinetable } from "@/api/businessInfo/coldefinetable";

export default {
  name: "Coldefinetable",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点位表格数据
      coldefinetableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        area: null,
        description: null,
        src: null,
        vartype: null,
        ip: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "点位名称不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "点位描述不能为空", trigger: "blur" }
        ],
        src: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        vartype: [
          { required: true, message: "数据类型不能为空", trigger: "change" }
        ],
        ip: [
          { required: true, message: "ip不能为空", trigger: "blur" }
        ],
      },
      importOpen: false,
      importUrl: process.env.VUE_APP_BASE_API + "/iot/coldefinetable/importVariablesVO" // ✅ 替换为你实际的上传地址
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询点位列表 */
    getList() {
      this.loading = true;
      listColdefinetable(this.queryParams).then(response => {
        this.coldefinetableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        area: null,
        description: null,
        src: null,
        vartype: null,
        ip: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加点位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getColdefinetable(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改点位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateColdefinetable(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addColdefinetable(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除点位编号为"' + ids + '"的数据项？').then(function() {
        return delColdefinetable(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iot/coldefinetable/exportVariablesVo', {
        ...this.queryParams
      }, `点位列表_${new Date().getTime()}.xlsx`)
    },
    handleImport() {
      this.importOpen = true;
    },
      /** 上传前校验 */
    beforeUpload(file) {
      const isXLSX = file.name.endsWith(".xlsx");
      if (!isXLSX) {
        this.$message.error("只能上传 .xlsx 文件");
        return false;
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("上传文件大小不能超过 2MB");
        return false;
      }
      return true;
    },

    /** 上传成功后回调 */
    handleImportSuccess(response) {
      if (response.code === 200) {
        this.$message.success("导入成功！");
        this.importOpen = false;
        this.getList(); // 重新加载表格数据
      } else {
        this.$message.error(response.msg || "导入失败！");
      }
    }

    
  }
};
</script>
